#include <string>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

class Record{
public:
    Record(const string & key,const string & value)
    :_key(key)
    ,_value(value)
    {

    }
    string _key;
    string _value;
};

class AliyunConfig{
public:
    static AliyunConfig * getInstance(){
        pthread_once(&_once,init);
        return _pInstance;
    }

private:
    class AutoRelease{
    public:
        AutoRelease(){}
        ~AutoRelease(){
            if(_pInstance){
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    };

    static AutoRelease _ar;
public:
    void put(const string & key,const string & value){
        auto at1 = _conf.begin();
        auto at2 = _conf.end();
        while(at1 != at2){
            if(at1->_key == key){
                at1->_value = value;
                return;
            }
            ++ at1;
        }
        Record record(key,value);
        _conf.push_back(record);
    }

    void get(const string & key){
        for(auto ele : _conf){
            if(ele._key == key){
                cout << ele._key << ":" << ele._value << endl;
                return;
            }
        }
    }
private:
    AliyunConfig(){}
    ~AliyunConfig(){}
    AliyunConfig(const AliyunConfig & rhs) = delete;
    AliyunConfig & operator=(const AliyunConfig & rhs) = delete;
    static AliyunConfig * _pInstance;
    static pthread_once_t _once;
    static void init(){
        _pInstance =  new AliyunConfig();
    }

    vector<Record> _conf;
};
AliyunConfig * AliyunConfig::_pInstance = nullptr;
pthread_once_t AliyunConfig::_once;
AliyunConfig::AutoRelease AliyunConfig::_ar;

void test(){
    ifstream ifs("aliyun.conf");
    if(!ifs){
        cerr << "file is not found" << endl;
        return;
    }

    string line;
    while(getline(ifs,line)){
        istringstream iss(line);
        string key,value;
        iss >> key >> value;
        AliyunConfig::getInstance()->put(key,value);
    }

    AliyunConfig::getInstance()->get("ip");
    AliyunConfig::getInstance()->get("port");

    AliyunConfig::getInstance()->put("port","8080");
    AliyunConfig::getInstance()->get("ip");
    AliyunConfig::getInstance()->get("port");
}

int main()
{
    test();
    return 0;
}

